package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.auth.model.Account;
import com.manage.config.Config;
import com.manage.model.Sdkconfig;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class SdkconfigServiceImpl extends AbstractService implements
		SdkconfigService {
	private Logger logger;
	
	public SdkconfigServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}

	@Override
	public Sdkconfig getSdkconfigById(int id) {
		String sql = "select * from sdkconfig where id=" + String.valueOf(id) + "";
		ModelGenerator<Sdkconfig> dbcpmg = new ModelGenerator<Sdkconfig>(Sdkconfig.class);
		return dbcpmg.generate(dbcp, sql);
	}

	@Override
	public Sdkconfig getSdkconfigBySdkprovidersid(int sdkprovidersid) {
		String sql = "select * from sdkconfig where sdkprovidersid=" + String.valueOf(sdkprovidersid) + "";
		ModelGenerator<Sdkconfig> dbcpmg = new ModelGenerator<Sdkconfig>(Sdkconfig.class);
		return dbcpmg.generate(dbcp, sql);
	}

	@Override
	public List<Sdkconfig> getSdkconfigs() {
		String sql = "select * from sdkconfig order by id desc";
		ModelGenerator<Sdkconfig> dbcpmg = new ModelGenerator<Sdkconfig>(Sdkconfig.class);
		return dbcpmg.generateList(dbcp, sql);
	}

	@Override
	public boolean saveSdkconfig(Account operator, String type, Sdkconfig model) {
		if (operator == null || operator.getId() == null || operator.getId().length() == 0)
			return false;
		String sql = "";
		if (type.equalsIgnoreCase("insert"))
		{
			model.setId(getTableId("sdkconfig"));
			sql = 
				"insert into sdkconfig(id,sdkprovidersid,operator,cmccprovince," +
				"unicomprovince,telecomprovince,feetype,feeceiling,feefloor,appointfee,createuser,createdate) " +
				"values(" + String.valueOf(model.getId()) + ",'" + model.getSdkprovidersid() + "','" + encodeDb(model.getOperator()) + 
				"','" + encodeDb(model.getCmccprovince()) + "','" +
						encodeDb(model.getUnicomprovince()) + "','" + model.getTelecomprovince() + "','" + 
						model.getFeetype() + "','" + 
						model.getFeeceiling() + "','" + 
						model.getFeefloor() + "','" + 
						model.getAppointfee() + "','" + 
						model.getCreateuser() + "','" + 
						model.getCreatedate() + "')";
			
		}
		else if (type.equalsIgnoreCase("update"))
		{
			sql = 
				"update sdkconfig set operator='" + encodeDb(model.getOperator()) + "'," +
						"telecomprovince='" + encodeDb(model.getTelecomprovince()) + "'," +
						"cmccprovince='" + encodeDb(model.getCmccprovince()) + "'," +
						"unicomprovince='" + encodeDb(model.getUnicomprovince()) + "'," +
						"feetype='" + model.getFeetype() + "'," +
						"feeceiling='" + model.getFeeceiling() + "'," +
						"feefloor='" + model.getFeefloor() + "'," +
						"appointfee='" + encodeDb(model.getAppointfee()) + "' " +
				" where id=" + String.valueOf(model.getId()) + "";
		}
		else if (type.equalsIgnoreCase("delete"))
		{
			sql = "delete from sdkconfig where id=" + String.valueOf(model.getId()) + "";
		}
		else
		{
			sql = "";
		}
		logger.debug("sql: " + sql);
		if (sql.length() > 0)
		{
			if (ConnectionManager.getInstance().execute(dbcp, sql))
			{
				Sdkconfig lo = null;
				if (!type.equals("delete"))
				{
					lo = getSdkconfigById(model.getId());
				}
				logTableOperate("sdkconfig", type, operator, lo);
				return true;
			}
		}		
		return false;
	}
}
